package com.duoqin.emoji;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ImageSpan;
import android.util.Log;

import java.lang.ref.WeakReference;
import java.util.concurrent.ConcurrentHashMap;

import pl.droidsonroids.gif.GifDrawable;

public class EmojiManager {
    private static final String TAG = "Duoqin.Emj";

    private static EmojiManager mInstance;
    private static ConcurrentHashMap<Integer, WeakReference<GifDrawable>> mEmotionCacheMap = null;
    private static String substring = "";

    private EmojiManager() {
        if(mEmotionCacheMap == null) {
            mEmotionCacheMap = new ConcurrentHashMap<>();
        }
    }

    public static EmojiManager getInstance() {
        if (null == mInstance) {
            mInstance = new EmojiManager();
        }
        return mInstance;
    }

    public SpannableString getExpressionString(Context context, String str, int textSize) {
//        Log.d(TAG, "str = " + str);
        SpannableString spannableString = new SpannableString(str);

        for (int i = 0; i < Emoji.EmojiPatterns.length; i++) {
            String PATTEN_STR = Emoji.EmojiPatterns[i];

            int indexOf = str.indexOf(PATTEN_STR);

            if (indexOf != -1) {
                try {
                    dealGifExpression(context, str, spannableString, textSize,
                            PATTEN_STR, Emoji.EmojiIcons[i],
                            indexOf);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return spannableString;
    }

    private void dealExpression(Context context, String str, SpannableString spannableString,
                                int textSize, String key, int icons, int start) throws Exception {
//        Log.d(TAG, spannableString + " == spannableString");

        Drawable d = context.getResources().getDrawable(icons);
        d.setBounds(0, 0, textSize, textSize);
        ImageSpan imageSpan = new ImageSpan(d);
        int end = start + key.length();
//        Log.d(TAG, "start =" + start);
        spannableString.setSpan(imageSpan, start, end,
                Spannable.SPAN_INCLUSIVE_EXCLUSIVE);

        int indexOf = spannableString.toString().substring(end).indexOf(key);
        if (indexOf != -1) {
//            Log.d(TAG, indexOf + "indexOf");
            dealExpression(context, substring, spannableString, textSize, key,
                    icons, end + indexOf);
        }
    }

    private void dealGifExpression(Context context, String str, SpannableString spannableString,
                                int textSize, String key, int icons, int start) throws Exception {
        GifDrawable gifDrawable = getEmotion(context.getResources(), icons);
        gifDrawable.setBounds(0, 0, textSize, textSize);
        GifImageSpan imageSpan = new GifImageSpan(gifDrawable);
        int end = start + key.length();
        spannableString.setSpan(imageSpan, start, end, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);

        int indexOf = spannableString.toString().substring(end).indexOf(key);
        if (indexOf != -1) {
            dealGifExpression(context, substring, spannableString, textSize, key,
                    icons, end + indexOf);
        }
    }

    private static GifDrawable getEmotion(Resources resources, int icon) throws Exception {
        WeakReference<GifDrawable> reference = mEmotionCacheMap.get(icon);
        if(reference != null && reference.get() != null) {
            return reference.get();
        } else {
            GifDrawable gifDrawable = new GifDrawable(resources, icon);
            reference = new WeakReference<>(gifDrawable);
            mEmotionCacheMap.put(icon, reference);
            return reference.get();
        }
    }
}
